package org.jawa.model;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;

import java.io.Serializable;

@Entity
@Table(name = "shop")
public class Shop extends BaseObject implements Serializable {
	
	private static final long serialVersionUID = -8149535570509923210L;
	private Long id;
	private User user;
	private String shopname;
	private Character shoptype;
	private Set<Order> orders = new HashSet<Order>(0);
	private Set<Product> products = new HashSet<Product>(0);

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "USERID")
	public User getUser() {
		return this.user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Column(name = "SHOPNAME", length = 50)
	public String getShopname() {
		return this.shopname;
	}

	public void setShopname(String shopname) {
		this.shopname = shopname;
	}

	@Column(name = "SHOPTYPE", length = 1)
	public Character getShoptype() {
		return this.shoptype;
	}

	public void setShoptype(Character shoptype) {
		this.shoptype = shoptype;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "shop")
	public Set<Order> getOrders() {
		return this.orders;
	}

	public void setOrders(Set<Order> orders) {
		this.orders = orders;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "shop")
	public Set<Product> getProducts() {
		return this.products;
	}

	public void setProducts(Set<Product> products) {
		this.products = products;
	}

	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || getClass() != o.getClass())
			return false;

		Shop pojo = (Shop) o;

		if (user != null ? !user.equals(pojo.user) : pojo.user != null)
			return false;
		if (shopname != null ? !shopname.equals(pojo.shopname)
				: pojo.shopname != null)
			return false;
		if (shoptype != null ? !shoptype.equals(pojo.shoptype)
				: pojo.shoptype != null)
			return false;

		return true;
	}

	public int hashCode() {
		int result = 0;
		result = (user != null ? user.hashCode() : 0);
		result = 31 * result + (shopname != null ? shopname.hashCode() : 0);
		result = 31 * result + (shoptype != null ? shoptype.hashCode() : 0);

		return result;
	}

	public String toString() {
		StringBuffer sb = new StringBuffer(getClass().getSimpleName());

		sb.append(" [");
		sb.append("id").append("='").append(getId()).append("', ");
		sb.append("user").append("='").append(getUser()).append("', ");
		sb.append("shopname").append("='").append(getShopname()).append("', ");
		sb.append("shoptype").append("='").append(getShoptype()).append("', ");

		sb.append("]");

		return sb.toString();
	}

}
